/****************************************************************
		*     COPYRIGHT Mobim Technology CO.,LTD     *
*****************************************************************/
/*****************************************************************
 * FileName::    mem.c
 * version::     0.01
 * Purpose:      gieve memory relative function
 * Authors:      Rafe Cao, 2007-9-30
 * Notes:		 <the limitations to use this file>
 ******************************************************************/
#include "stdio.h"
#include <windows.h>
#include ".\utilities\utilities.h"

typedef unsigned char uint8_t;
typedef signed char int8_t;
typedef unsigned short uint16_t;
typedef signed short int16_t;
typedef signed int int32_t;
typedef unsigned int uint32_t;

extern "C" void *memcpy(void *dest, const void *src, uint32_t count);
extern "C" void *memset(void *dest, int32_t c,uint32_t count);

#define Mii_Memcpy	memcpy
#define Mii_Memset	memset

//#define MII_MTK_PLATFORM
/**
 * Free memory which has been allocated with av_malloc(z)() or av_realloc().
 * NOTE: ptr = NULL is explicetly allowed
 * Note2: it is recommended that you use av_freep() instead
 */
#ifdef MII_MTK_PLATFORM
extern void *ime_mem_alloc(uint32_t size);
extern void ime_mem_free(void *ptr);
extern void *ime_tcm_alloc(uint32_t size);
extern void ime_tcm_free(void *ptr);
extern void *ime_int_alloc(uint32_t size);
extern void ime_int_free(void *ptr);
#endif

extern "C"
{
void mii_trace(const char* text, ...);
void mii_assert(int s, const char* str/*=0*/, ...);
void *mii_mem_alloc(uint32_t size);
void mii_mem_free(void *ptr);
void *mii_tcm_alloc(uint32_t size);
void mii_tcm_free(void *ptr);
void *mii_int_alloc(uint32_t size);
void mii_int_free(void *ptr);
}

void mii_trace(const char* text, ...)
{
#ifdef IMITV_DEBUG_VERSION
	char buf[200] = {0};
	TCHAR BUF[200] = {0};

    va_list params;
    va_start(params, text);

	_vsnprintf(buf, 198, text, params);
	UTF8ToUni(buf, BUF);
	OutputDebugStringW(BUF);

    va_end (params);
#endif
}
void mii_assert(int s, const char* str/*=0*/, ...)
{
#ifdef IMITV_DEBUG_VERSION
	if(!s){
		static int no = 0;
		char buf[200] = {0};
		if(str)
		{
			va_list params;
			va_start(params, str);
			_vsnprintf(buf, 198, str, params);
			va_end (params);
		}
		no ++;
		if(no>10)
			no = 1;
		mii_trace("%d. Assertion - %s", no, buf);
	}
#endif
}

void *mii_mem_alloc(uint32_t size)
{
    void *ptr;
#ifdef MII_MTK_PLATFORM
	ptr = (void*)ime_mem_alloc(size);
#else
	ptr = (void*)new uint32_t[(size+3)>>2];
#endif
	if (ptr)
	   Mii_Memset(ptr,0,size);
    return ptr;
}
void mii_mem_free(void *ptr)
{
	if (ptr)
	{
#ifdef MII_MTK_PLATFORM
		ime_mem_free(ptr);
#else
		delete [] (uint32_t*)ptr;
#endif
	}
}
void *mii_tcm_alloc(uint32_t size)
{
    void *ptr;
#ifdef MII_MTK_PLATFORM
	ptr = ime_tcm_alloc(size);
#else
	ptr = mii_mem_alloc(size);
#endif
	if (ptr)
	   Mii_Memset(ptr,0,size);
	return ptr;
}
void mii_tcm_free(void *ptr)
{
#ifdef MII_MTK_PLATFORM
	ime_tcm_free(ptr);
#else
	mii_mem_free(ptr);
#endif
}
void *mii_int_alloc(uint32_t size)
{
	void *ptr;
#ifdef MII_MTK_PLATFORM
	ptr = ime_int_alloc(size);
#else
	ptr = mii_mem_alloc(size);
#endif
	if (ptr)
	   Mii_Memset(ptr,0,size);
	return ptr;
}
void mii_int_free(void *ptr)
{
#ifdef MII_MTK_PLATFORM
	ime_int_free(ptr);
#else
	mii_mem_free(ptr);
#endif
}

